﻿using System.Diagnostics;
using System.Globalization;
using System.Text;
using MediatR;
using Microsoft.AspNetCore.Http;

namespace Xman.Operation.Http;
public class OperationMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IMediator _mediator;

    public OperationMiddleware(RequestDelegate next, IMediator mediator)
    {
        _next = next;
        _mediator= mediator;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var stime = DateTime.Now;
        context.Request.EnableBuffering();
        await _mediator.Publish(new OperationEvent { EventId = Trace.CorrelationManager.ActivityId, Context = context, StartTime = stime });
        await _next(context);
    }
}